package com.miwzn.boot.component.config.shiro;

import com.miwzn.boot.modules.sys.entity.po.SysPermission;
import com.miwzn.boot.modules.sys.entity.po.SysRole;
import com.miwzn.boot.modules.sys.entity.po.SysUser;
import com.miwzn.comm.constant.MyConstant;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Auther: wzn
 * @Date: 2018/6/19 17:36
 * @Description:
 */
public class ShiroUser extends SysUser {
    /**
     * 当前角色
     */
    private SysRole role;
    /**
     * 角色
     */
    private List<SysRole> roles;
    /**
     * 权限
     */
    private List<SysPermission> permissions;

    private boolean isSuperAdmin = Boolean.FALSE;

    public SysRole getRole() {
        return role;
    }

    public void setRole(SysRole role) {
        this.role = role;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public List<SysPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<SysPermission> permissions) {
        this.permissions = permissions;
    }

    public Set<String> getStringRoles() {
        Set<String> stringSet = new HashSet<>();
        if (role != null) {
            if (StringUtils.isNotBlank(role.getCode())) {
                String[] split = role.getCode().split(",");
                stringSet.addAll(Arrays.asList(split));
            }
        }
        return stringSet;
    }

    public Set<String> getStringPermissions() {
        Set<String> stringSet = new HashSet<>();
        if (CollectionUtils.isNotEmpty(permissions)) {
            for (SysPermission permission : permissions) {
                if (StringUtils.isNotBlank(permission.getPermissionCode())) {
                    String[] split = permission.getPermissionCode().split(",");
                    stringSet.addAll(Arrays.asList(split));
                }
            }
        }
        return stringSet;
    }

    public boolean isSuperAdmin() {
        if ((role != null && role.getCode().equalsIgnoreCase(MyConstant.UserRole.SUPER_ADMIN.value)) || super.getId().equals(MyConstant.SUPER_ADMIN_ID)) {
                return !isSuperAdmin;
        }
        return isSuperAdmin;
    }
}
